package cn.zahuo.SJR.jdbc.UserLogin;
import cn.zahuo.SJR.jdbc.JDBC_tools.Jdbc_tools;

import java.sql.*;

public class userlogin {
//    登陆方法
    public  boolean login(String username,String password){
        Connection connection=null;
//        PreparedStatement防止SQLz注入问题
        PreparedStatement pstmt=null;
        ResultSet rs=null;
//        判断输入是否为空
        if(username==null || password==null){
            return  false;
        }
        try {
            connection= Jdbc_tools.getConnection();
            String sql = "select * from user where username =? and password = ?";
//           执行sql对象
            pstmt=connection.prepareStatement(sql);
//            给?赋值
            pstmt.setString(1,username);
            pstmt.setString(2,password);
//可以传参数，可空参
            rs=pstmt.executeQuery();
            return  rs.next();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            Jdbc_tools.close(rs,pstmt,connection);
        }
        return  false;
    }
}
